package hk.guosen.weix.example.svc;

import java.util.ArrayList;
import java.util.List;

import hk.guosen.weix.shared.msg.Article;
import hk.guosen.weix.svc.AppService;
import hk.guosen.weix.svc.msg.event.EventMsg;
import hk.guosen.weix.svc.msg.event.EventMsgClick;
import hk.guosen.weix.svc.msg.user.BaseMsg;
import hk.guosen.weix.svc.msg.user.ReqMsgText;
import hk.guosen.weix.svc.msg.user.RespMsg;
import hk.guosen.weix.svc.msg.user.RespMsgNews;
import hk.guosen.weix.svc.msg.user.RespMsgText;

public class WeixinService implements AppService {
	
	public  WeixinService()
	{
		
	}

	@Override
	public RespMsg onMsg(BaseMsg msg) throws Exception {
		// TODO Auto-generated method stub
		if(msg.isTextMsg())
		{//如果请求的是文本消息则处理
			ReqMsgText req = (ReqMsgText) msg;
			//使用请求文本构建一个应答消息
			RespMsgText result = new RespMsgText(req, req.getContent());
			return result;
		}
		else if(msg.isEventMsg())
		{   //事件消息处理
			
			EventMsg ReqEventMsg=(EventMsg)msg;
			if(ReqEventMsg.isClickEvent())
			{
				
			  EventMsgClick ReqEventMsgClick=(EventMsgClick)msg;
			  if(ReqEventMsgClick.getEventKey().equals("query"))
			  {
				  List<Article> articles=new ArrayList<Article>();
				  Article art1=new Article("bus365", "测试bus365<a href='weixin://contacts/profile/gh_a2f27cde340a'>关注bus365</a>", "http://www.bus365.com/public/images/image2.0/logo.png", "weixin://contacts/profile/gh_a2f27cde340a");
				  articles.add(art1);
				  RespMsg result=new RespMsgNews(msg,articles) ;
				  return result;
			  }
			  else
			  {
			    RespMsg result=new RespMsgText(ReqEventMsgClick,ReqEventMsgClick.getEventKey()) ;
			    return result;
			  }
			}
			return null;
		}
		else
		{
			return null;
		}
	}

}
